package leetcode.violence;

import java.util.ArrayList;
import java.util.List;

public class Permutation {
    public static void main(String[] args) {
        String string = "abacd";

        List<String> res = new ArrayList<>();

        if (string == null || string.length() == 0){
            System.out.println("");
        }
        char[] chars = string.toCharArray();
        process(chars,0,res);
        res.sort(null);
        for (String str:res) {
            System.out.println(str);
        }

    }
    public static void process(char[] chars,int i,List res){
        if (i == chars.length){
            res.add(String.valueOf(chars));
        }
        boolean[] visit = new boolean[26];
        for (int j = i; j <chars.length ; j++) {
            if (!visit[chars[j]-'a']) {
                visit[chars[j]-'a']=true;
                swap(chars, i, j);
                process(chars, i + 1, res);
                swap(chars, i, j);
            }
        }
    }
    public static void swap(char[] chars,int i,int j){
        char temp = chars[i];
        chars[i] = chars[j];
        chars[j] = temp;
    }

}
